#include "unix_socket_server.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>

#include <ut/thread.h>
#include <ut/eventloop.h>

using namespace ut;

class UnixSocketServerPrivate
{
public:
    UnixSocketServerPrivate(const std::string &serverName)
        : m_listening(false)
        , m_serverName(serverName)
        , m_acceptThread(new Thread(std::bind(&UnixSocketServerPrivate::accept, this)))
        , m_recivehread(new Thread(std::bind(&UnixSocketServerPrivate::readMessage, this)))
    {
        // TODO: 如果serverName为空呢？
        unlink(serverName.c_str()); // 解除原有serverName对象链接

        // 配置server_address
        m_serverAddress.sun_family = AF_UNIX;
    }

    virtual ~UnixSocketServerPrivate()
    {
        close();
    }

    void close()
    {
        if (m_listening) {
            ::close(m_serverSockfd);
            unlink(m_serverName.c_str());
        }
    }
    bool isListening() const
    {
        return m_listening;
    }

    bool listen(const std::string &serverName = "server_socket")
    {
        m_serverName = serverName;
        m_serverSockfd = socket(AF_UNIX, SOCK_STREAM, 0); // 创建socket，指定通信协议为AF_UNIX,数据方式SOCK_STREAM
        //配置server_address
        strcpy(m_serverAddress.sun_path, m_serverName.c_str());

        bind(m_serverSockfd, (struct sockaddr *)&m_serverAddress, sizeof(m_serverAddress));
        m_listening = ::listen(m_serverSockfd, 5) == 0;
        if (m_listening) {
        }
        return m_listening;
    }

    void accept()
    {
        int client_sockfd;
        int client_len;
        struct sockaddr_un client_address;
        int i, byte;
        char ch_send, recv_buf[128];

        client_len = sizeof(client_address);

        while (m_listening) {
            // accept函数接收客户端求情，存储客户端地址信息、客户端地址大小
            client_sockfd = ::accept(m_serverSockfd, (struct sockaddr *)&client_address, (socklen_t *)&client_len);
            if (client_sockfd == -1) {
                continue;
            }

            Channel
        }
        // for (i = 0, ch_send = '1'; i < 5; i++, ch_send++) {
        //     //从client_sockfd读取客户端发来的消息
        //     if ((byte = read(client_sockfd, recv_buf, sizeof(recv_buf))) == -1) {
        //         perror("read");
        //         exit(EXIT_FAILURE);
        //     }
        //     printf("the massage receiver from client is: %s\n", recv_buf);
        //     sleep(1);
        //     //向客户端发送消息
        //     if ((byte = write(client_sockfd, &ch_send, 1)) == -1) {
        //         perror("write");
        //         exit(EXIT_FAILURE);
        //     }
        // }
    }

    void readMessage()
    {
        EventLoop a;

                a.loop();
    }

private:
    bool m_listening;
    int m_serverSockfd;
    std::string m_serverName;
    struct sockaddr_un m_serverAddress;
    std::shared_ptr<Thread> m_acceptThread;
    std::shared_ptr<Thread> m_recivehread;
};

UnixSocketServer::UnixSocketServer(const std::string &serverName)
    : m_serverPrivate(new UnixSocketServerPrivate(serverName))
{
}

void UnixSocketServer::close()
{
    m_serverPrivate->close();
}
bool UnixSocketServer::isListening() const
{
    return m_serverPrivate->isListening();
}
bool UnixSocketServer::listen(const std::string &serverName = "server_socket")
{
    return m_serverPrivate->listen(serverName);
}
